﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAO_Layer;
using System.Net.Mail;
 

namespace Controller
{
    public class UserController
    {
        public static Usuario loggedUser;

        public void setLoggedUser(Usuario u)
        {
            loggedUser = u;
        }

        public Usuario getUserLogged()
        {
            return loggedUser;
        }

        public String registerUser(Usuario u,int rol_id)
        {
            StringBuilder sb = new StringBuilder();
            DAOFactory daofactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            DAO_Usuarios daoUsuario = daofactory.getDaoUsuario();

            if (! daoUsuario.validEmail(u.Email))
            {
                sb.Append("Email ya existe. ");
            }

            if (! daoUsuario.validUser(u.Username))
            {
                sb.Append("El usuario ya existe. ");
            }

            if (sb.Length == 0)
            {
                String pass = u.Password;
                String user = u.Username;
                  
                u.hashPassword();
                daoUsuario.add(u,rol_id);
                String body = getBodyNewUser(user, pass);
                this.sendMail("Cuenta Registrada", u.Email, body);
            }
            
            return sb.ToString();
        }

        private void sendMail(String subject,String email,String body) 
        {
            SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("sistemadeplanillasYOLO@gmail.com", "20092049");


            MailMessage mm = new MailMessage("sistemasdeplanillas@soltec.com", email, subject, "");
            mm.Body = body;          
            

            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(mm);
        }

        private String getBodyNewUser(String u, String p)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Bienvenido al Sitema de Planillas\n");
            sb.Append("Su usuario es : "+u+"\n");
            sb.Append("Su password es : "+p+"\n");
            
            return sb.ToString();
        }
        public List<Rol> getAllRoles()
        {
            List<Rol> roles;

            DAOFactory daofactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            DAO_Roles daoRoles = daofactory.getDaoRoles();

            roles = daoRoles.queryAll();

            return roles;
        }

        public bool validCredentials(Usuario u)
        {
            DAOFactory daofactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            DAO_Usuarios daUser = daofactory.getDaoUsuario();

            Usuario user = daUser.validCredentials(u);

            if (user == null)
            {
                return false;
            }              
            else
            {
                setLoggedUser(user);
                return true;
            }

        }

        public void sendPassword(String email)
        {
            DAOFactory daofactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            DAO_Usuarios daUser = daofactory.getDaoUsuario();

            Usuario user = daUser.getUserbyEmail(email);

            if (user != null)
            {
                user.unHashPassword();
                String body = getForgotPasswordBody(user.Username, user.Password);
                this.sendMail("Olvido Contraseña", user.Email, body);

            }

            
        }

        public String getForgotPasswordBody(String u, String p)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Olvido Contraseña\n");
            sb.Append("Su usuario es : " + u + "\n");
            sb.Append("Su password es : " + p + "\n");

            return sb.ToString();
        }
    }
}
